import React, { useRef } from 'react';
import { connect } from 'umi';
import TableDataBlock from '@/components/TableDataBlock';
import { reloadTab } from '@/utils/utils';
import { notification } from 'antd';
import { Button, Modal, Tag, Space } from 'antd';
import { AdminType } from '@/common/contact';
import { uploadFile, setStagePre } from '@/utils/utils'
const AuthFormSetting = (props) => {
  const {
    visible = false,
    onClose,
    loading,
    organizationAuthAdminList,
    dispatch,
    uploadList = [],
    handleCallback,
    searchType = '',
    onBack
  } = props;
  const { show = false, type = 'keyword', id, values = {} } = visible;
  const { stage, mainStage } = values
  const requesturl = {
    keyword: 'user/fakeKeywordAssignAdmin',
    user: 'user/fakeUserAssignAdmin',
    video: 'user/fakeVideoAssignAdmin',
    comment: 'user/fakeCommentAssignAdmin',
    allComment: 'user/fakeCommentAssignAdmin',
    userAdmin: 'user/fakeUserAssignListAdmin',
    userAllAdmin: 'user/fakeUserAssignListAdmin',
    batchUserStage: 'user/fakeBatchChangeUserStage',
    userStage: 'user/fakeChangeUserStage'
  }[type];
  const childRef = useRef();

  const searchItems = [
    {
      label: '员工姓名',
      name: 'keyword',
    },
  ];
  // 确认提交
  const handleUpAudit = () => { };
  const getColumns = [
    {
      title: '姓名',
      dataIndex: 'username',
    },
    {
      title: '岗位类型',
      dataIndex: 'adminType',
      render: (e) => {
        return AdminType[e];
      },
    },
    {
      type: 'handle',
      width: 200,
      dataIndex: 'id',
      render: (val, record) => [
        {
          title: '分配员工',
          type: 'up',
          click: () => {
            if (type === 'comment') {
              dispatch({
                type: requesturl,
                payload: {
                  commentIdList: [id],
                  assignAdminId: val,
                },
                callback: (e) => {
                  onClose();
                  handleCallback();
                },
              });
            } else if (type === 'allComment') {
              if (uploadList.length === 0) {
                return notification.warn({
                  message: '温馨提示',
                  description: `请先选择要分配的视频`,
                });
              }
              dispatch({
                type: requesturl,
                payload: {
                  commentIdList: uploadList,
                  assignAdminId: val,
                },
                callback: (e) => {
                  onClose();
                  handleCallback();
                },
              });
            } else if (type === 'userAdmin') {
              dispatch({
                type: requesturl,
                payload: {
                  userIdList: [id],
                  assignAdminId: val,
                  ...values
                },
                callback: (e) => {
                  onClose();
                  handleCallback();
                },
              });
            } else if (type === 'userAllAdmin') {
              if (uploadList.length === 0) {
                return notification.warn({
                  message: '温馨提示',
                  description: `请先选择要分配的视频`,
                });
              }
              dispatch({
                type: requesturl,
                payload: {
                  ...values,
                  userIdList: uploadList,
                  assignAdminId: val,
                },
                callback: (e) => {
                  const { allSuccessFlag, failAmount, successAmount, url } = e
                  if (allSuccessFlag === '1') {
                    onClose();
                    handleCallback();
                  } else {
                    Modal.confirm({
                      zIndex: 2000,
                      title: '分配结果',
                      content: `成功分配${successAmount}个，失败${failAmount},下载表格查看详情`,
                      okText: '下载',
                      onOk() {
                        if (url) {
                          uploadFile(url)
                        }

                        onClose();
                        handleCallback();

                      },
                      onCancel() {
                        onClose();
                        handleCallback();
                        console.log('Cancel');
                      },
                    })
                  }

                },
              });
            } else if (type === 'batchUserStage') {
              if (uploadList.length === 0) {
                return notification.warn({
                  message: '温馨提示',
                  description: `请先选择要流转的用户`,
                });
              }
              dispatch({
                type: requesturl,
                payload: {
                  organizationUserIdList: uploadList,
                  organizationAdminId: val,
                },
                callback: (e) => {
                  onClose();
                  handleCallback();
                },
              });
            } else if (type === 'userStage') {
              dispatch({
                type: requesturl,
                payload: {
                  organizationUserId: id,
                  organizationAdminId: val,
                  ...values
                },
                callback: (e) => {
                  onClose();
                  handleCallback();
                },
              });
            } else {
              dispatch({
                type: requesturl,
                payload: {
                  id,
                  assignAdminId: val,
                },
                callback: (e) => {
                  onClose();
                  handleCallback();
                },
              });
            }
          },
        },
      ],
    },
  ];
  const modalProps = {
    title: `请选择您需要分配的账号`,
    width: 900,
    visible: typeof visible === 'boolean' ? visible : show,
    maskClosable: true,
    destroyOnClose: true,
    zIndex: 1001,
    confirmLoading: loading,
    footer: type === 'userStage' ? <Space>
      <Button onClick={() => onClose()}>取消</Button>
      <Button onClick={() => onBack && onBack()}>上一步</Button>
    </Space> : <Button onClick={() => onClose()}>取消</Button>,
  };

  return (
    <Modal {...modalProps} onCancel={onClose} onOk={handleUpAudit}>
      <TableDataBlock
        searchItems={searchItems}
        cRef={childRef}
        loading={loading}
        noCard={true}
        columns={getColumns}
        params={{
          searchType,
          stage: mainStage ? '' : stage,
          invalidStatus: '1',
          mainStage,
        }}
        rowKey={(record) => `${record.id}`}
        dispatchType="weCom/fetchSearchOrganizationAdmin"
        {...organizationAuthAdminList}
      ></TableDataBlock>
    </Modal>
  );
};

export default connect(({ weCom, loading }) => ({
  ...weCom,
  loading: loading.models.weCom || loading.models.user,
}))(AuthFormSetting);
